package ldz.Show;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import ldz.User.User;
import ldz.User.UserDao;
import ldz.User.UserDaoImpl;

import java.util.List;
import java.awt.Color;

public class UserManagementGUI extends JFrame {
    private JTextField userNoTextField;
    private JButton deleteButton;
    private JLabel resultLabel;

    // 新增用于插入用户信息的组件
    private JTextField unoTextField;
    private JTextField unameTextField;
    private JTextField usexTextField;
    private JTextField upasswordTextField;
    private JTextField utelTextField;
    private JTextField uemailTextField;
    private JButton insertButton;

    // 新增用于查询所有用户信息的组件
    private JTextArea allUsersTextArea;
    private JButton queryAllButton;

    // 新增用于更新用户信息的组件
    private JTextField updateUnoTextField;
    private JTextField updatePasswordTextField;
    private JTextField updateEmailTextField;
    private JButton updateButton;

    public UserManagementGUI() {
        // 设置窗口标题
        setTitle("用户管理系统");
        // 设置窗口大小并使其不可调整
        setSize(600, 400);
        setResizable(false);
        // 设置窗口关闭时的操作（这里是退出程序）
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        // 创建选项卡面板
        JTabbedPane tabbedPane = new JTabbedPane();

        // 1. 创建删除用户选项卡面板
        JPanel deletePanel = createDeleteUserPanel();
        tabbedPane.addTab("删除用户", deletePanel);

        // 2. 创建插入用户选项卡面板
        JPanel insertPanel = createInsertUserPanel();
        tabbedPane.addTab("插入用户", insertPanel);

        // 3. 创建查询所有用户选项卡面板（进一步优化此处，扩大滚动显示范围）
        JPanel queryAllPanel = createQueryAllUsersPanel();
        tabbedPane.addTab("查询所有用户", queryAllPanel);

        // 4. 优化创建更新用户选项卡面板
        JPanel updatePanel = createUpdateUserPanel();
        tabbedPane.addTab("更新用户", updatePanel);

        // 将选项卡面板添加到窗口中
        add(tabbedPane);

        // 设置窗口可见
        setVisible(true);
    }

    private JPanel createDeleteUserPanel() {
        // 此处代码保持不变，和原来一样创建删除用户选项卡面板
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        JPanel inputPanel = new JPanel(new GridLayout(2, 2, 5, 5));
        inputPanel.setBorder(BorderFactory.createTitledBorder("输入要删除用户的编号"));

        JLabel label = new JLabel("用户编号：");
        label.setFont(new Font("宋体", Font.BOLD, 14));
        label.setForeground(Color.DARK_GRAY);
        inputPanel.add(label);

        userNoTextField = new JTextField();
        userNoTextField.setFont(new Font("宋体", Font.PLAIN, 14));
        inputPanel.add(userNoTextField);

        deleteButton = new JButton("删除用户");
        deleteButton.setFont(new Font("宋体", Font.BOLD, 14));
        deleteButton.setForeground(Color.WHITE);
        deleteButton.setBackground(Color.RED);
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String userNo = userNoTextField.getText().trim();
                if (!userNo.isEmpty()) {
                    UserDao userDao = new UserDaoImpl();
                    int result = userDao.deleteUser(userNo);
                    if (result > 0) {
                        resultLabel.setText("用户删除成功！");
                    } else {
                        resultLabel.setText("用户删除失败，请检查编号是否正确。");
                    }
                } else {
                    resultLabel.setText("请输入有效的用户编号！");
                }
            }
        });
        inputPanel.add(deleteButton);

        JPanel resultPanel = new JPanel();
        resultPanel.setBackground(Color.WHITE);
        resultLabel = new JLabel("");
        resultLabel.setFont(new Font("宋体", Font.BOLD, 14));
        resultPanel.add(resultLabel);

        panel.add(inputPanel, BorderLayout.NORTH);
        panel.add(resultPanel, BorderLayout.CENTER);

        return panel;
    }

    private JPanel createInsertUserPanel() {
        // 此处代码保持不变，和原来一样创建插入用户选项卡面板
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        JPanel inputPanel = new JPanel(new GridLayout(7, 2, 5, 5));
        inputPanel.setBorder(BorderFactory.createTitledBorder("输入用户信息"));

        JLabel unoLabel = new JLabel("用户编号：");
        unoLabel.setFont(new Font("宋体", Font.BOLD, 14));
        unoLabel.setForeground(Color.DARK_GRAY);
        inputPanel.add(unoLabel);
        unoTextField = new JTextField();
        unoTextField.setFont(new Font("宋体", Font.PLAIN, 14));
        inputPanel.add(unoTextField);

        JLabel unameLabel = new JLabel("用户名：");
        unameLabel.setFont(new Font("宋体", Font.BOLD, 14));
        unameLabel.setForeground(Color.DARK_GRAY);
        inputPanel.add(unameLabel);
        unameTextField = new JTextField();
        unameTextField.setFont(new Font("宋体", Font.PLAIN, 14));
        inputPanel.add(unameTextField);

        JLabel usexLabel = new JLabel("性别：");
        usexLabel.setFont(new Font("宋体", Font.BOLD, 14));
        usexLabel.setForeground(Color.DARK_GRAY);
        inputPanel.add(usexLabel);
        usexTextField = new JTextField();
        usexTextField.setFont(new Font("宋体", Font.PLAIN, 14));
        inputPanel.add(usexTextField);

        JLabel upasswordLabel = new JLabel("密码：");
        upasswordLabel.setFont(new Font("宋体", Font.BOLD, 14));
        upasswordLabel.setForeground(Color.DARK_GRAY);
        inputPanel.add(upasswordLabel);
        upasswordTextField = new JTextField();
        upasswordTextField.setFont(new Font("宋体", Font.PLAIN, 14));
        inputPanel.add(upasswordTextField);

        JLabel utelLabel = new JLabel("电话：");
        utelLabel.setFont(new Font("宋体", Font.BOLD, 14));
        utelLabel.setForeground(Color.DARK_GRAY);
        inputPanel.add(utelLabel);
        utelTextField = new JTextField();
        utelTextField.setFont(new Font("宋体", Font.PLAIN, 14));
        inputPanel.add(utelTextField);

        JLabel uemailLabel = new JLabel("邮箱：");
        uemailLabel.setFont(new Font("宋体", Font.BOLD, 14));
        uemailLabel.setForeground(Color.DARK_GRAY);
        inputPanel.add(uemailLabel);
        uemailTextField = new JTextField();
        uemailTextField.setFont(new Font("宋体", Font.PLAIN, 14));
        inputPanel.add(uemailTextField);

        insertButton = new JButton("插入用户");
        insertButton.setFont(new Font("宋体", Font.BOLD, 14));
        insertButton.setForeground(Color.WHITE);
        insertButton.setBackground(Color.GREEN);
        insertButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String uno = unoTextField.getText().trim();
                String uname = unameTextField.getText().trim();
                String usex = usexTextField.getText().trim();
                String upassword = upasswordTextField.getText().trim();
                String utel = utelTextField.getText().trim();
                String uemail = uemailTextField.getText().trim();
                UserDao userDao = new UserDaoImpl();
                int result = userDao.insertUser(uno, uname, usex, upassword, utel, uemail);
                if (result > 0) {
                    resultLabel.setText("用户插入成功！");
                } else {
                    resultLabel.setText("用户插入失败，请检查输入信息是否正确。");
                }
            }
        });
        inputPanel.add(insertButton);

        JPanel resultPanel = new JPanel();
        resultPanel.setBackground(Color.WHITE);
        resultLabel = new JLabel("");
        resultLabel.setFont(new Font("宋体", Font.BOLD, 14));
        resultPanel.add(resultLabel);

        panel.add(inputPanel, BorderLayout.NORTH);
        panel.add(resultPanel, BorderLayout.CENTER);

        return panel;
    }

    private JPanel createQueryAllUsersPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 添加用于输入查询条件的文本框和标签
        JPanel inputPanel = new JPanel(new GridLayout(2, 2, 5, 5));
        JLabel conditionLabel = new JLabel("查询条件（留空则查询所有用户）:");
        conditionLabel.setFont(new Font("宋体", Font.BOLD, 14));
        conditionLabel.setForeground(Color.DARK_GRAY);
        inputPanel.add(conditionLabel);

        JTextField conditionTextField = new JTextField();
        inputPanel.add(conditionTextField);

        JButton queryButton = new JButton("查询");
        queryButton.setFont(new Font("宋体", Font.BOLD, 14));
        queryButton.setForeground(Color.WHITE);
        queryButton.setBackground(Color.BLUE);
        queryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String condition = conditionTextField.getText().trim();
                UserDao userDao = new UserDaoImpl();
                if (condition.isEmpty()) {
                    // 查询所有用户
                    List<User> userList = userDao.getAllUsers();
                    StringBuilder sb = new StringBuilder();
                    for (User user : userList) {
                        sb.append("用户编号: ").append(user.getUno()).append("\n");
                        sb.append("用户名: ").append(user.getUname()).append("\n");
                        sb.append("性别: ").append(user.getUsex()).append("\n");
                        sb.append("密码: ").append(user.getUpassword()).append("\n");
                        sb.append("电话: ").append(user.getUtel()).append("\n");
                        sb.append("邮箱: ").append(user.getUemail()).append("\n\n");
                    }
                    allUsersTextArea.setText(sb.toString());
                } else {
                    // 查询特定用户
                    List<User> userList = userDao.queryUserByUno(condition);
                    StringBuilder sb = new StringBuilder();
                    for (User user : userList) {
                        sb.append("用户编号: ").append(user.getUno()).append("\n");
                        sb.append("用户名: ").append(user.getUname()).append("\n");
                        sb.append("性别: ").append(user.getUsex()).append("\n");
                        sb.append("密码: ").append(user.getUpassword()).append("\n");
                        sb.append("电话: ").append(user.getUtel()).append("\n");
                        sb.append("邮箱: ").append(user.getUemail()).append("\n\n");
                    }
                    allUsersTextArea.setText(sb.toString());
                }
            }
        });

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(queryButton);

        // 创建滚动面板，将文本域放置在滚动面板中
        JScrollPane scrollPane = new JScrollPane();
        JPanel textAreaPanel = new JPanel();

        // 调整文本域的初始大小，设置更多的行数和合适的列数，使其能容纳更多内容
        allUsersTextArea = new JTextArea(20, 40);
        allUsersTextArea.setFont(new Font("宋体", Font.PLAIN, 14));
        allUsersTextArea.setEditable(false);
        // 设置文本域自动换行，避免内容超出显示范围时出现显示问题
        allUsersTextArea.setLineWrap(true);
        allUsersTextArea.setWrapStyleWord(true);

        // 将文本域添加到滚动面板中
        scrollPane.setViewportView(allUsersTextArea);
        textAreaPanel.add(scrollPane);

        panel.add(inputPanel, BorderLayout.NORTH);
        panel.add(buttonPanel, BorderLayout.SOUTH);  // 将按钮面板放置在底部
        panel.add(textAreaPanel, BorderLayout.CENTER);

        return panel;
    }

    private JPanel createUpdateUserPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 上部面板，用于放置输入更新用户信息的组件，使用更合理的GridBagLayout布局，便于灵活控制组件位置和间距
        JPanel inputPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5); // 设置组件间距
        gbc.fill = GridBagConstraints.HORIZONTAL; // 设置组件水平填充，确保能完整显示

        // 用户编号输入相关
        JLabel updateUnoLabel = new JLabel("用户编号：");
        updateUnoLabel.setFont(new Font("宋体", Font.BOLD, 14));
        updateUnoLabel.setForeground(Color.DARK_GRAY);
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0.2; // 分配一定权重，用于布局调整
        inputPanel.add(updateUnoLabel, gbc);

        updateUnoTextField = new JTextField(10); // 设置合适的显示宽度
        updateUnoTextField.setFont(new Font("宋体", Font.PLAIN, 14));
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 0.8; // 分配一定权重，用于布局调整
        inputPanel.add(updateUnoTextField, gbc);

        // 新密码输入相关
        JLabel updatePasswordLabel = new JLabel("新密码：");
        updatePasswordLabel.setFont(new Font("宋体", Font.BOLD, 14));
        updatePasswordLabel.setForeground(Color.DARK_GRAY);
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 0.2; // 分配一定权重，用于布局调整
        inputPanel.add(updatePasswordLabel, gbc);

        updatePasswordTextField = new JTextField(10); // 设置合适的显示宽度
        updatePasswordTextField.setFont(new Font("宋体", Font.PLAIN, 14));
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.weightx = 0.8; // 分配一定权重，用于布局调整
        inputPanel.add(updatePasswordTextField, gbc);

        // 新邮箱输入相关
        JLabel updateEmailLabel = new JLabel("新邮箱：");
        updateEmailLabel.setFont(new Font("宋体", Font.BOLD, 14));
        updateEmailLabel.setForeground(Color.DARK_GRAY);
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.weightx = 0.2; // 分配一定权重，用于布局调整
        inputPanel.add(updateEmailLabel, gbc);

        updateEmailTextField = new JTextField(10); // 设置合适的显示宽度
        updateEmailTextField.setFont(new Font("宋体", Font.PLAIN, 14));
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.weightx = 0.8; // 分配一定权重，用于布局调整
        inputPanel.add(updateEmailTextField, gbc);

        // 创建更新按钮，设置字体、颜色、背景色等样式，并添加点击事件监听器
        updateButton = new JButton("更新用户");
        updateButton.setFont(new Font("宋体", Font.BOLD, 14));
        updateButton.setForeground(Color.WHITE);
        updateButton.setBackground(Color.ORANGE);
        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String uno = updateUnoTextField.getText().trim();
                String newPassword = updatePasswordTextField.getText().trim();
                String newEmail = updateEmailTextField.getText().trim();
                UserDao userDao = new UserDaoImpl();
                int result = userDao.updateUser(uno, newPassword, newEmail);
                if (result > 0) {
                    resultLabel.setText("用户信息更新成功！");
                } else {
                    resultLabel.setText("用户信息更新失败，请检查输入信息是否正确。");
                }
            }
        });
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 2; // 跨两列显示，使按钮居中
        gbc.weightx = 1.0; // 占满整行宽度
        inputPanel.add(updateButton, gbc);

        // 中部面板，用于放置显示结果的标签，设置背景色和字体等样式
        JPanel resultPanel = new JPanel();
        resultPanel.setBackground(Color.WHITE);
        resultLabel = new JLabel("");
        resultLabel.setFont(new Font("宋体", Font.BOLD, 14));
        resultPanel.add(resultLabel);

        panel.add(inputPanel, BorderLayout.NORTH);
        panel.add(resultPanel, BorderLayout.CENTER);

        return panel;
    }

    public static void main(String[] args) {
        new UserManagementGUI();
    }
}